package casts.audio
{
   import flash.events.Event;
   import flash.media.Sound;
   import flash.media.SoundChannel;
   import flash.utils.getDefinitionByName;
   
   import jp.progression.casts.CastMovieClip;

   /**
    * 音樂物件，用在按鈕rollover時播放音效，又不想重疊音效
    * @author cjboy1984@gmail.com
    * @usage
    * // 省略前段singleton碼…
    * DEF.sound_1.playSound();
    */
   public class Sound_1 extends CastMovieClip
   {
      private var snd:Sound;
      private var sndChannel:SoundChannel;
      private var _playing:Boolean; // 是否播放中

      public function Sound_1( initObject:Object = null )
      {
         super( initObject );

         // 初始化sound
         var cls:Class = getDefinitionByName('sound_2') as Class;
         snd = new cls() as Sound;
         _playing = false;

         stop();
      }

      // --------------------- LINE ---------------------

      // 控制是否mute掉
      public function playSound():void
      {
         if (!_playing)
         {
            _playing = true;
            sndChannel = snd.play(0, 0, new SoundTransform(1));
            sndChannel.addEventListener(Event.SOUND_COMPLETE, soundEnd);
         }
      }

      // 是否播放中
      public function get playing():Boolean { return _playing; }

      // 或是可以直接存取soundChannel
      public function get soundChannel():SoundChannel { return sndChannel; }

      // ################### protected ##################

      override protected function atCastAdded():void
      {
      }

      override protected function atCastRemoved():void
      {
      }

      // #################### private ###################

      private function soundEnd(e:Event):void
      {
         _playing = false;
      }

      // --------------------- LINE ---------------------

   }

}
